package com.zabra.zl.ui.index_fragment.index;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.TabAdapter;
import com.zabra.zl.base.BaseMvpFragment;
import com.zabra.zl.data.IndexMenuBean;
import com.zabra.zl.ui.allocation_manage.AllocationManageActivity;
import com.zabra.zl.ui.allocation_plan.AllocationPlanActivity;
import com.zabra.zl.ui.index_fragment.index.contract.IndexFragmentContract;
import com.zabra.zl.ui.index_fragment.index.fragment.dayhot.DayHot;
import com.zabra.zl.ui.index_fragment.index.fragment.informationcenter.InformationCenter;
import com.zabra.zl.ui.index_fragment.index.presenter.IndexFragmentPresenter;
import com.zabra.zl.ui.runing.RunningActivity;
import com.zabra.zl.ui.search.SearchActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class IndexFragment extends BaseMvpFragment<IndexFragmentPresenter> implements IndexFragmentContract.View {

    @BindView(R.id.xtab)
    TabLayout xtab;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    Unbinder unbinder;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    Unbinder unbinder1;
    @BindView(R.id.tvs_search)
    TextView tvsSearch;
    Unbinder unbinder2;
    private List<String> titleList;
    private List<Fragment> fragmentList;
    private List<IndexMenuBean> list = new ArrayList<>();
    private IndexMenuAdapter adapter;
    private Bundle bundle = new Bundle();

    @Override
    protected void initView(View view) {
        titleList = new ArrayList<>();
        titleList.add("今日热点");
        titleList.add("资讯中心");
        fragmentList = new ArrayList<>();
        fragmentList.add(new DayHot());
        fragmentList.add(new InformationCenter());

        viewpager.setAdapter(new TabAdapter(getActivity().getSupportFragmentManager(), titleList, fragmentList));
        viewpager.setOffscreenPageLimit(0);
        xtab.setupWithViewPager(viewpager);
        TabLayout.Tab tabAt = xtab.getTabAt(0);
        tabChangSize(tabAt);
        viewpager.setCurrentItem(0);
        xtab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewpager.setCurrentItem(tab.getPosition());
                tabChangSize(tab);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        setData();
        adapter = new IndexMenuAdapter(R.layout.item_index_menu, list);
        recycler.setLayoutManager(new GridLayoutManager(getActivity(), 4, LinearLayoutManager.VERTICAL, false));
        recycler.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (position) {
                    case 0:
                        //免息
                        bundle.putInt("mIndex", Config.FREE);
                        toClass(getActivity(), AllocationPlanActivity.class, bundle);
                        break;
                    case 1:
                        //按日
                        bundle.putInt("mIndex", Config.DAY);
                        toClass(getActivity(), AllocationPlanActivity.class, bundle);
                        break;
                    case 2:
                        //按月
                        bundle.putInt("mIndex", Config.MON);
                        toClass(getActivity(), AllocationPlanActivity.class, bundle);
                        break;
                    case 3:
                        //资金流水
                        toClass(getActivity(), RunningActivity.class);
                        break;
                    case 4:
                        //配资管理
                        toClass(getActivity(), AllocationManageActivity.class);
                        break;

                }
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_index;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    private void setData() {
        IndexMenuBean bean1 = new IndexMenuBean();
        IndexMenuBean bean2 = new IndexMenuBean();
        IndexMenuBean bean3 = new IndexMenuBean();
        IndexMenuBean bean4 = new IndexMenuBean();
        IndexMenuBean bean5 = new IndexMenuBean();
        IndexMenuBean bean6 = new IndexMenuBean();
        IndexMenuBean bean7 = new IndexMenuBean();
        bean1.setmIcon(R.mipmap.icon_mianxi);
        bean1.setmText("免息配资");
        bean2.setmIcon(R.mipmap.icon_day);
        bean2.setmText("按日配资");
        bean3.setmIcon(R.mipmap.icon_month);
        bean3.setmText("按月配资");
        bean4.setmIcon(R.mipmap.icon_liushui);
        bean4.setmText("资金流水");
        bean5.setmIcon(R.mipmap.icon_manage);
        bean5.setmText("配资管理");
        bean6.setmIcon(R.mipmap.icon_tuiguang);
        bean6.setmText("推广");
        bean7.setmIcon(R.mipmap.icon_more);
        bean7.setmText("更多");
        list.add(bean1);
        list.add(bean2);
        list.add(bean3);
        list.add(bean4);
        list.add(bean5);
        list.add(bean6);
        list.add(bean7);
    }

    //tab选中是改变字体大小
    private void tabChangSize(TabLayout.Tab tab) {
        TextView textView = new TextView(getActivity());
        float selectedSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 18, getResources().getDisplayMetrics());
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, selectedSize);
        TextPaint paint = textView.getPaint();
        paint.setFakeBoldText(true);
        textView.setText(tab.getText());
        tab.setCustomView(textView);
    }


    @OnClick(R.id.tvs_search)
    public void onViewClicked() {
        toClass(getActivity(), SearchActivity.class);
    }
}
